package net.zjitc.redis;
import com.google.gson.Gson;
import java.util.List;
import net.zjitc.dao.AdminDao;
import net.zjitc.dao.CategoryDao;
import net.zjitc.dao.ProductDao;
import net.zjitc.dao.UserCartDao;
import net.zjitc.pojo.CartItem;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.CategorySecond;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/8 14:28
 * @description:
 */
@Component
public class NoSql {
  @Autowired
  public NoSql(Redis redis,CategoryDao categoryDao,ProductDao productDao,UserCartDao userCartDao,AdminDao adminDao) {
    Gson gson = new Gson();
    //用户存入缓存
    List<User> userList = adminDao.findAllUser();
    String UserList = gson.toJson(userList);
    redis.set("user", UserList);
    //一级菜单存入缓存
    List<Category> categoryList =categoryDao.findAll();
    for (Category category : categoryList){
      List<CategorySecond> categorySecondList = categoryDao.findCategorySecondByCid(category.getCid());
      for (CategorySecond categorySecond : categorySecondList){
        List<Product> productList = productDao.findPageByCsid(categorySecond.getCsid());
        categorySecond.setProductList(productList);
      }
      category.setSecondList(categorySecondList);
    }
    String CategoryList =gson.toJson(categoryList);
    redis.set("category",CategoryList);
    //二级菜单存入缓存
    List<CategorySecond> categorySecondList =categoryDao.findAllSecond();
    String CategorysecondList =gson.toJson(categorySecondList);
    redis.set("categorysecond",CategorysecondList);
    //商品存入缓存
    List<Product> productList = productDao.adminProduct();
    String ProductList = gson.toJson(productList);
    redis.set("product", ProductList);
    //购物车中的商品存入缓存
    List<CartItem> cartItems =userCartDao.findAll();
    String cartlist = gson.toJson(cartItems);
    redis.set("cartitem",cartlist);
  }
}
